home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 May: Tool Chest / Dev.CD May 00 TC.toast / pc / tool chest / macapp / ad lib 3.0.1 / install ad lib 3.0.1 / Ad Lib 3.0.1 / MacApp Extensions / UCmdKeyBehavior.cpp / UCmdKeyBehavior.cpp
Encoding:
Text File  |  1998-05-29  |  8.0 KB  |  394 lines  |  [TEXT/CWIE]

  1. //    UCmdKeyBehavior.cp
  2. //    Copyright © 1992-1995 Nick Nallick.  All rights reserved.
  3. //
  4. //
  5. //    Changes:
  6. //
  7. //        •    Respect the dim and enabled states of controls.
  8. //        •    Add MacApp 3.3 compatibility.
  9. //        •    Add MacApp 3.1 compatibility.
  10. //
  11.  
  12. #ifndef __UCmdKeyBehavior__
  13. #include "UCmdKeyBehavior.h"
  14. #endif
  15.  
  16. #pragma segment CmdKeyBehavior
  17.  
  18.  
  19. #if qMacAppVersion > 30
  20. MA_DEFINE_CLASS_M1(TCmdKeyBehavior, TBehavior);
  21. #endif
  22.  
  23.  
  24. #ifdef HANDLE_BASED_OBJECTS
  25.  
  26. //    Overrides TObject.
  27. //    Initialize the fields.
  28. //
  29. MACAPP_METHOD
  30. void TCmdKeyBehavior::Initialize()
  31. {
  32.     fKey = 0;
  33.     fShiftedKey = 0;
  34.     fCommandKeyOnly = FALSE;
  35.     fBackupOwner = NULL;
  36.     TBehavior::Initialize();
  37. }
  38.  
  39. #elif qMacAppVersion <= 33
  40.  
  41. //    Initialize the fields.
  42. //
  43. TCmdKeyBehavior::TCmdKeyBehavior()
  44. {
  45.     fKey = 0;
  46.     fShiftedKey = 0;
  47.     fCommandKeyOnly = FALSE;
  48.     fBackupOwner = NULL;
  49. }
  50.  
  51. #else
  52.  
  53. //    Initialize the fields.
  54. //
  55. TCmdKeyBehavior::TCmdKeyBehavior(IDType itsIdentifier, char cmdKey) :
  56.     TBehavior(itsIdentifier),
  57.     fKey(0),
  58.     fShiftedKey(0),
  59.     fCommandKeyOnly(FALSE),
  60.     fBackupOwner(NULL)
  61. {
  62.     SetKey(cmdKey);
  63. }
  64.  
  65. // Added destructor    --    MB-9805-29-1948
  66. TCmdKeyBehavior::~TCmdKeyBehavior()
  67. {
  68. }
  69.  
  70. #endif
  71.  
  72. #if qMacAppVersion <= 33
  73.  
  74. //    Initialize the behavior.
  75. //
  76. //    cmdKey:        the command key value
  77. //
  78. MACAPP_METHOD
  79. void TCmdKeyBehavior::ICmdKeyBehavior(IDType itsIdentifier, char cmdKey)
  80. {
  81.     IBehavior(itsIdentifier);
  82.     SetKey(cmdKey);
  83. }
  84.  
  85. #endif
  86. //    Overrides TBehavior.
  87. //    Handle the command key events.
  88. //
  89. MACAPP_METHOD
  90. void TCmdKeyBehavior::DoCommandKeyEvent(TToolboxEvent* event)
  91. {
  92.     TBehavior::DoCommandKeyEvent(event);
  93.  
  94.     if (TToolboxEvent_GetCharacter(event) == fKey || TToolboxEvent_GetCharacter(event) == fShiftedKey)
  95.         SendDefaultEvent();
  96. }
  97.  
  98.  
  99. //    Overrides TBehavior.
  100. //    Handle the key events.
  101. //
  102. MACAPP_METHOD
  103. void TCmdKeyBehavior::DoKeyEvent(TToolboxEvent* event)
  104. {
  105.     TBehavior::DoKeyEvent(event);
  106.  
  107.     if (!fCommandKeyOnly && (TToolboxEvent_GetCharacter(event) == fKey || TToolboxEvent_GetCharacter(event) == fShiftedKey))
  108.         SendDefaultEvent();
  109. }
  110.  
  111.  
  112. //    Overrides TObject.
  113. //    Return the standard signature.
  114. //    This way if the user overrides the class, we'll still know what it is.
  115. //
  116. MACAPP_METHOD
  117. IDType TCmdKeyBehavior::GetStandardSignature() CONST
  118. {
  119.     return kCmdKeySignature;
  120. }
  121.  
  122.  
  123. //    Overrides TObject.
  124. //    Read the fields.
  125. //
  126. MACAPP_METHOD
  127. void TCmdKeyBehavior::ReadFrom(TStream* stream)
  128. {
  129.     TBehavior::ReadFrom(stream);
  130.  
  131.     char itsCmdKey = stream->ReadByte();
  132.     Boolean itsCmdKeyState = stream->ReadBoolean();
  133.     SetKey(itsCmdKey);
  134.     SetCmdKeyOnly(itsCmdKeyState);
  135. }
  136.  
  137.  
  138. //    Overrides TBehavior.
  139. //    Maintain the backup owner field.
  140. //
  141. MACAPP_METHOD
  142. void TCmdKeyBehavior::SetOwner(TEventHandler* itsOwner)
  143. {
  144.     TBehavior::SetOwner(itsOwner);
  145.     fBackupOwner = itsOwner;
  146. }
  147.  
  148.  
  149. //    Overrides TObject.
  150. //    Write the fields.
  151. //
  152. MACAPP_METHOD
  153. void TCmdKeyBehavior::WriteTo(TStream* stream) CONST
  154. {
  155.     TBehavior::WriteTo(stream);
  156.  
  157.     stream->WriteByte(fKey);
  158.     stream->WriteBoolean(fCommandKeyOnly);
  159. }
  160.  
  161.  
  162. //    Get the key value.
  163. //
  164. //    NO PARAMETERS
  165. //
  166. //    RETURNS:    the key value
  167. //
  168. MACAPP_METHOD
  169. char TCmdKeyBehavior::GetKey()
  170. {
  171.     return fKey;
  172. }
  173.  
  174.  
  175. //    Set the key value.
  176. //
  177. //    newKey:        the new key value
  178. //
  179. MACAPP_METHOD
  180. void TCmdKeyBehavior::SetKey(char newKey)
  181. {
  182.     if (newKey >= 'A' && newKey <= 'Z')        // NOT case sensitive
  183.     {
  184.         fKey = newKey - 'A' + 'a';
  185.         fShiftedKey = newKey;
  186.     }
  187.     else
  188.     {
  189.         fKey = newKey;
  190.         fShiftedKey = newKey - 'a' + 'A';
  191.     }
  192. }
  193.  
  194.  
  195. //    Get the command key use state.
  196. //
  197. //    NO PARAMETERS
  198. //
  199. //    RETURNS:    TRUE if the behavior only responds to command keys; FALSE otherwise
  200. //
  201. MACAPP_METHOD
  202. Boolean TCmdKeyBehavior::DoesCmdKeyOnly()
  203. {
  204.     return fCommandKeyOnly;
  205. }
  206.  
  207.  
  208. //    Set the command key use state.
  209. //
  210. //    useCmdKeyOnly:    if TRUE, only responds to command key events;
  211. //                                    otherwise responds to all key events
  212. //
  213. MACAPP_METHOD
  214. void TCmdKeyBehavior::SetCmdKeyOnly(Boolean useCmdKeyOnly)
  215. {
  216.     fCommandKeyOnly = useCmdKeyOnly;
  217. }
  218.  
  219.  
  220. //    Send the default event number to the owner.
  221. //
  222. //    NO PARAMETERS
  223. //
  224. MACAPP_METHOD
  225. void TCmdKeyBehavior::SendDefaultEvent()
  226. {
  227.     fOwner = fBackupOwner;    // replace the owner so the default event propagates
  228.     if (fOwner && !((TControl*) fOwner)->IsDimmed())
  229.     {
  230.         ((TControl*) fOwner)->Flash();
  231.         fOwner->HandleEvent(((TControl*) fOwner)->GetEventNumber(), fOwner, NULL);
  232.     }
  233. }
  234.  
  235.  
  236. ////////////////////////////// TBroadcastCmdKey //////////////////////////////
  237. //
  238. //    A behavior which passes command key events to all the owners subviews.
  239. //
  240.  
  241. class TBroadcastCmdKey : public TBehavior
  242. {
  243. #if qMacAppVersion > 30
  244.     MA_DECLARE_CLASS;
  245. #endif
  246.  
  247.     public:
  248.         VIRTUAL void        DoCommandKeyEvent(TToolboxEvent* event);
  249.         VIRTUAL void        DoKeyEvent(TToolboxEvent* event);
  250. #if qMacAppVersion <= 33
  251.         VIRTUAL void        IBroadcastCmdKey(IDType itsIdentifier);
  252. #else
  253.                                         TBroadcastCmdKey(IDType itsIdentifier = kNoIdentifier_AC);
  254.         virtual                ~    TBroadcastCmdKey();
  255. #endif
  256.  
  257.     private:
  258.         static    void        BroadcastCmd(TView* view, TToolboxEvent* event, Boolean cmdKey);
  259. };
  260.  
  261. #if qMacAppVersion > 30
  262. MA_DEFINE_CLASS_M1(TBroadcastCmdKey, TBehavior);
  263. #endif
  264.  
  265.  
  266. //    Overrides TBehavior.
  267. //    Handle the command key events.
  268. //
  269. MACAPP_METHOD
  270. void TBroadcastCmdKey::DoCommandKeyEvent(TToolboxEvent* event)
  271. {
  272.     TBehavior::DoCommandKeyEvent(event);
  273.  
  274.     if (!event->IsAutoKeyEvent())
  275.     {
  276.         CSubViewIterator iter((TView*) fOwner);
  277.         for (TView* subview = iter.FirstSubView(); iter.Current(); subview = iter.NextSubView())
  278.             BroadcastCmd(subview, event, TRUE);
  279.     }
  280. }
  281.  
  282.  
  283. //    Overrides TBehavior.
  284. //    Handle the key events.
  285. //
  286. MACAPP_METHOD
  287. void TBroadcastCmdKey::DoKeyEvent(TToolboxEvent* event)
  288. {
  289.     TBehavior::DoKeyEvent(event);
  290.  
  291.     if (!event->IsAutoKeyEvent())
  292.     {
  293.         CSubViewIterator iter((TView*) fOwner);
  294.         for (TView* subview = iter.FirstSubView(); iter.Current(); subview = iter.NextSubView())
  295.             BroadcastCmd(subview, event, FALSE);
  296.     }
  297. }
  298.  
  299. #if qMacAppVersion <= 33
  300.  
  301. //    Initialize the behavior.
  302. //
  303. //    cmdKey:        the command key value
  304. //
  305. MACAPP_METHOD
  306. void TBroadcastCmdKey::IBroadcastCmdKey(IDType itsIdentifier)
  307. {
  308.     IBehavior(itsIdentifier);
  309. }
  310.  
  311. #else
  312.  
  313. //    Initialize the behavior.
  314. //
  315. //    cmdKey:        the command key value
  316. //
  317. TBroadcastCmdKey::TBroadcastCmdKey(IDType itsIdentifier) :
  318.     TBehavior(itsIdentifier)
  319. {
  320. }
  321.  
  322. // Added destructor    --    MB-9805-29-1950
  323. TBroadcastCmdKey::~TBroadcastCmdKey()
  324. {
  325. }
  326.  
  327. #endif
  328.  
  329.  
  330. //    Recursively send the command to a view's behaviors and its subview's behaviors.
  331. //
  332. //    view:        the top level view
  333. //    event:    the key event
  334. //    cmdKey:    if TRUE, this is a command key event; otherwise it's a plain key event
  335. //
  336. void TBroadcastCmdKey::BroadcastCmd(TView* view, TToolboxEvent* event, Boolean cmdKey)
  337. {
  338.     // Before sending the event to the behavior chain we disconnect it from its
  339.     // owner to prevent the event from coming back up the target chain to the 
  340.     // behavior and causing an infinite loop.  This is accomplished by temporarily
  341.     // setting the owner of the last behavior in the chain to NULL.
  342.     // TCmdKeyBehavior saves a backup copy of its owner so it still knows
  343.     // where to send the message.
  344.  
  345.     if (view->IsEnabled())
  346.     {
  347.         TBehavior* firstBehavior = view->GetFirstEnabledBehavior();
  348.         if (firstBehavior)
  349.         {
  350.             // first find the last behavior in the chain
  351.             TBehavior* lastBehavior = firstBehavior;
  352.             TBehavior* nextBehavior = firstBehavior->GetNextEnabledBehavior();
  353.             while (nextBehavior)
  354.             {
  355.                 lastBehavior = nextBehavior;
  356.                 nextBehavior = nextBehavior->GetNextEnabledBehavior();
  357.             }
  358.  
  359.             // next, save the owner, set it to NULL, and send the event
  360.             TEventHandler* owner = TBehavior_GetOwner(lastBehavior);
  361.             TBehavior_SetOwner(lastBehavior, NULL);
  362.             if (cmdKey)
  363.                 firstBehavior->DoCommandKeyEvent(event);
  364.             else
  365.                 firstBehavior->DoKeyEvent(event);
  366.  
  367.             // finally, reset the owner
  368.             TBehavior_SetOwner(lastBehavior, owner);
  369.         }
  370.     }
  371.  
  372.     // send it to the subviews
  373.     CSubViewIterator iter(view);
  374.     for (TView* subview = iter.FirstSubView(); iter.ITERCURRENT(); subview = iter.NextSubView())
  375.         BroadcastCmd(subview, event, cmdKey);
  376. }
  377.  
  378.  
  379. //////////////////////////////////////////////////////////////////////////////
  380.  
  381. #pragma segment AInit
  382.  
  383. void RegisterTCmdKeyBehavior();        // function prototype
  384.  
  385. void RegisterTCmdKeyBehavior()
  386. {
  387. #if qMacAppVersion == 30
  388.     new TCmdKeyBehavior;
  389.     new TBroadcastCmdKey;
  390. #else
  391.     MA_REGISTER_SIGNATURE(TCmdKeyBehavior, kCmdKeySignature);
  392.     MA_REGISTER_CLASS(TBroadcastCmdKey);
  393. #endif
  394. }